﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BDS3M.Admin
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {            
                if (!IsPostBack)
                {
                    txtUserAdmin.Attributes.Add("onkeypress", "return clickButton(event,'" + btnLogin.ClientID + "')");
                    txtPasswordAdmin.Attributes.Add("onkeypress", "return clickButton(event,'" + btnLogin.ClientID + "')");
                    txtUserForget.Attributes.Add("onkeypress", "return clickButton(event,'" + btnForget.ClientID + "')");
                    txtEmailForget.Attributes.Add("onkeypress", "return clickButton(event,'" + btnForget.ClientID + "')");
                }                  
        }

        protected void btnLogin_Click1(object sender, EventArgs e)
        {
           
                string loginuser = txtUserAdmin.Text;
                string loginpass = txtPasswordAdmin.Text;
                string matkhaulogin = LIB.Converter.Encode("sha1", loginpass);
                DataTable loginaccount = DAL.DCollector.DBCollector.Instance.StoredProcedureLogin("loginaccount", loginuser, matkhaulogin);

                if (loginaccount.Rows.Count == 0)
                {
                    Response.Redirect("~/Admin/Login.aspx");
                }
                else
                {
                    Session["Account_Role"] = loginaccount.Rows[0]["Account_Role"].ToString();

                    Session["Account_ID"] = loginaccount.Rows[0]["Account_ID"].ToString();
                    //Session["Account_Email"] = loginaccount.Rows[0]["Account_Email"].ToString();
                    Session["Account_Avatar"] = loginaccount.Rows[0]["Account_Avatar"].ToString();
                    //Session["Account_Address"] = loginaccount.Rows[0]["Account_Address"].ToString();
                    //Session["Account_Phonenumber"] = loginaccount.Rows[0]["Account_Phonenumber"].ToString();
                    //Session["Account_FullName"] = loginaccount.Rows[0]["Account_FullName"].ToString();
                    Session["Account_QL1"] = loginaccount.Rows[0]["Account_QL1"].ToString();
                    Session["Account_QL2"] = loginaccount.Rows[0]["Account_QL2"].ToString();
                    Session["Account_QL3"] = loginaccount.Rows[0]["Account_QL3"].ToString();
                    Session["Account_QL4"] = loginaccount.Rows[0]["Account_QL4"].ToString();
                    Session["Account_QL5"] = loginaccount.Rows[0]["Account_QL5"].ToString();
                    Session["Account_QL6"] = loginaccount.Rows[0]["Account_QL6"].ToString();
                    Session["UsernameAccount"] = txtUserAdmin.Text;
                    Session["PasswordAccount"] = txtPasswordAdmin.Text;

                    if (chkGhinho.Checked)
                    {
                        HttpCookie cki = new HttpCookie("CookieUser");
                        cki.Value = txtUserAdmin.Text;
                        Response.Cookies.Add(cki);
                        cki.Expires = DateTime.MaxValue;
                        Response.Redirect("~/Admin/Index.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/Admin/Index.aspx");
                    }
                }                 
        }

        protected void btnForget_Click(object sender, EventArgs e)
        {
            try
            {
                string username = txtUserForget.Text;
                string email = txtEmailForget.Text;
                DataTable forgetpassword = DAL.DCollector.DBCollector.Instance.StoredProcedureForgetPassword("forgetpasswordaccount", username, email);
                if (forgetpassword.Rows[0]["result"].ToString() != "0")
                {
                    string randomPass = LIB.Converter.RandomString(15).ToUpper();
                    string newpass = LIB.Converter.Encode("sha1", randomPass);
                    DAL.DHelpers.Column[] cl = new DAL.DHelpers.Column[1];
                    cl[0] = new DAL.DHelpers.Column("Account_Password", randomPass, DAL.DHelpers.DataType.DataTypeSql.Varchar);
                    string[] colWhere = new string[1];
                    colWhere[0] = "Account_Email";

                    object[] colValue = new object[1];
                    colValue[0] = newpass;
                    DAL.DCollector.DBCollector.Instance.StoredProcedureUpdateByRow("tbl_Account", "Account_Email", email, "Account_Password", newpass);
                    LIB.Validate.SendMail(email, "Đây là password mới của bạn . Bạn vui lòng đăng nhập vào thay đổi password :\n" + randomPass);
                    Response.Redirect("~/Admin/Login.aspx");
                }
                else
                {
                    lblCheck.Text = "Bạn vui lòng kiểm tra lại Tài khoản & Email";
                }            
            }
            catch (Exception)
            {
                Response.Redirect("/Admin/error.htm");
            }                         
        }
    }
}